package com.sudao.android.candle;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

import java.io.File;

import com.sudao.android.candle.CandleView.CandleThread;

public class CandleActivity extends Activity {

	private static final String KEY_POSITION = "position";

    private static final int MENU_PLAY = 1;
    private static final int MENU_MUSIC = 2;
    private static final int MENU_SETTING = 3;
    private static final int MENU_ABOUT = 4;

	private static final int REQUEST_ABOUT = 0;
	private static final int REQUEST_SETTING = 1;

    private CandleView mCandleView;

	FileBrowser mFileBrowser;
	PlayList mPlayList = new PlayList(this);
	public MusicPlayer mMusicPlayer = new MusicPlayer();

	public static final int DIALOG_FILE_BROWSER = 1;
	public static final int DIALOG_PLAY_REMOVE = 2;

	public int mOperatedSongIndex;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_PLAY, 0, R.string.menu_play);
        menu.add(0, MENU_MUSIC, 0, R.string.menu_music);
        menu.add(0, MENU_SETTING, 0, R.string.menu_setting);
        menu.add(0, MENU_ABOUT, 0, R.string.menu_about);

        return true;
    }

	public Dialog onCreateDialog(int id) {
		switch (id) {
			case DIALOG_FILE_BROWSER:
				mFileBrowser = new FileBrowser(this, this);
				mFileBrowser.setPlayList(mPlayList);
				return mFileBrowser;
			case DIALOG_PLAY_REMOVE:
				return new AlertDialog.Builder(CandleActivity.this)
					.setTitle(R.string.play_remove)
					.setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						if (which == 0) {
							mFileBrowser.playFromPlayList(mOperatedSongIndex);
							Log.d("___________", "play " + mOperatedSongIndex);
						} else if (which == 1) {
							mFileBrowser.removeFromPlayList(mOperatedSongIndex);
							Log.d("___________", "remove  " + mOperatedSongIndex);
						}
					}
			}).create();
		}
		return null;
	}

	private int mCurrentPosition;
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_MUSIC:
				showDialog(DIALOG_FILE_BROWSER);
                return true;
            case MENU_PLAY:
				if (mMusicPlayer.mPlaying) {
					mCurrentPosition = mMusicPlayer.getCurrentPosition();
					mMusicPlayer.pauseMedia();
				} else {
					mMusicPlayer.playMedia();
					mMusicPlayer.setCurrentPosition(mCurrentPosition);
				}
                return true;
            case MENU_SETTING:
				startActivityForResult(new Intent(this, Setting.class), REQUEST_SETTING);
                return true;
            case MENU_ABOUT:
                Util.openLocalPage(this, "file:///android_asset/menu_help.html", REQUEST_ABOUT);
				return true;
        }

        return false;
    }

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem item = menu.findItem(MENU_PLAY);
		if (mMusicPlayer.mPlaying) {
			item.setTitle(R.string.menu_pause);	
		} else {
			item.setTitle(R.string.menu_play);
		}
		return true;
	}

	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && data != null) {
            switch (requestCode) {
            case REQUEST_ABOUT:
                break;
            case REQUEST_SETTING:
                boolean multi = data.getExtras().getBoolean(Setting.MULTI);
                String shape  = data.getExtras().getString(Setting.SHAPE);
                int count = data.getExtras().getInt(Setting.COUNT);
				applySetting(multi, shape, count);
                break;
            }
        }
    }
	
	private void applySetting(boolean multi, String shape, int count) {
		if (mCandleView != null && mCandleView.getThread() != null)
			mCandleView.getThread().applySetting(multi, shape, count);	
	}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

		final Window win = getWindow();
		win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);	

        setContentView(R.layout.candle_layout);

        mCandleView = (CandleView) findViewById(R.id.lunar);

		mMusicPlayer.setCandleActivity(this);
		mMusicPlayer.start();

        if (savedInstanceState == null) {
        } else {
            mCandleView.getThread().restoreState(savedInstanceState);
			mMusicPlayer.setCurrentPosition(savedInstanceState.getInt(KEY_POSITION));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mCandleView.getThread().pause(); 
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mCandleView.getThread().saveState(outState);
		outState.putInt(KEY_POSITION, mMusicPlayer.getCurrentPosition());
    }

	@Override
	protected void onDestroy() {
		mMusicPlayer.destroyMedia();
		super.onDestroy();
	}

	private void copyMusic() {
		String folder = this.getDir("certs", Context.MODE_PRIVATE).getPath() + "/";
		String filePath = folder + "eastern.mp3";
		File file = new File(filePath);
		if (file.exists()) {
			return;
		} else {
			
		}
	}
}
